<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>


  <script>
    let loginList = [
      { userName: 'admin', password: 'admin321' },
      { userName: 'zhangsan', password: 'zs321' },
      { userName: 'lisi', password: 'ls321' },
      { userName: 'yyqx', password: 'yyqx321' }
    ]

    let userInfo = {
      token0: {
        city: '杭州',
        money: '1100w',
        sex: '女',
        userName: 'admin',
        password: 'admin321'
      },
      token1: {
        city: '杭州',
        money: '1000w',
        sex: '女',
        userName: 'admin',
        password: 'admin321'
      },
      token2: {
        city: '北京',
        money: '1100w',
        sex: '女',
        userName: 'zhangsan',
        password: 'zs321'
      },
      token3: {
        city: '北京',
        money: '800w',
        sex: '男',
        userName: 'lisi',
        password: 'ls321'
      },
      token4: {
        city: '天津',
        money: '600w',
        sex: '男',
        userName: 'yyqx',
        password: 'yyqx321'
      }
    }



    let login = function (arrList, infoList) {
      let uname = prompt('请输入用户名')
      let pwd = prompt('请输入密码')
      let index = Math.floor(Math.random() * 5)
      let token = 'token'
      token = token + index
      // token = token + i

      // for (let k in arrList) {
        for (let i = 0; i < arrList.length; i++) {
        if (uname === arrList[i].userName && pwd === arrList[i].password) {
          alert(`
          登陆成功，具体用户信息:

          city:${userInfo.token1.city}
          money:${userInfo.token1.money}
          sex:${userInfo.token1.sex}
          userName:${uname}
          `)

          console.log(arrList[i]);
          break
        } else{
          let num = parseInt(prompt(`账号不存在

          1.注册
          2.直接退出`))
          // break

          // num === 1 注册 收集用户信息 追加到loginList里
          if (num === 1) {
            let newName = prompt('请输入新的用户名')
            let newPassword = prompt('请输入密码')
            let newObj = {
              userName: newName,
              password: newPassword
            }
            arrList.push(newObj)
            // alert('注册成功')
            // console.log(arrList);
            // 弹出注册成功 重复登录过程
            //等一下考虑封装几个函数(用户信息验证) 再重复调用 
            let nName = prompt('注册成功,请输入用户名')
            let npwd = prompt('请输入密码')
            // 这里向上面一样便利数组没有判断成功
            if (nName === newName && npwd === newPassword) {
              alert(`
              登陆成功，具体用户信息:

              city:${userInfo[token].city}
              money:${userInfo[token].money}
              sex:${userInfo[token].sex}
              userName:${nName}
            `)
              break;
            } else {
              //这次登陆失败 弹出无机会 break
              alert('完了完了，刚注册的就输错了，没机会了')
              break
            }

          } else if (num === 2) {
            // num === 2 break
            break
          } else {
            alert('请输入正确的选项')
          }
        }
      }
    }
    // login(loginList, userInfo)
    // console.log(userInfo['token' + 'index']);
    console.log(login(loginList, userInfo));
  </script>
</body>

</html>